/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VentanaEmpresa.java
 *
 * Created on 30/07/2011, 01:16:07 AM
 */

package co.srm.vistas.funcionales;

import co.srm.entidades.Empresa;
import co.srm.manejadores.ManejadorEmpresa;
import co.srm.utilidades.Control;
import javax.swing.JOptionPane;

/**
 *
 * @author JORGE
 */
public class VentanaEmpresa extends javax.swing.JDialog {

    public static Empresa empresast ;
    
    /** Creates new form VentanaEmpresa */
    public VentanaEmpresa(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
        botonModificar.setEnabled(false);
    }

       /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jXPanel6 = new org.jdesktop.swingx.JXPanel();
        jLabel14 = new javax.swing.JLabel();
        jXPanel1 = new org.jdesktop.swingx.JXPanel();
        botonGuardar = new javax.swing.JButton();
        botonModificar = new javax.swing.JButton();
        botonEmpresas = new javax.swing.JButton();
        ChecSelect = new javax.swing.JCheckBox();
        panelEmpresa = new org.jdesktop.swingx.JXPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        campoNombre = new javax.swing.JTextField();
        campoTelefono = new javax.swing.JTextField();
        campoCiudad = new javax.swing.JTextField();
        campoCorreo = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        campoDireccion = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jXPanel6.setBackground(new java.awt.Color(255, 220, 98));
        jXPanel6.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(255, 255, 255), 1, true));
        jXPanel6.setForeground(new java.awt.Color(255, 255, 255));

        jLabel14.setFont(new java.awt.Font("Arial", 3, 18));
        jLabel14.setText("Registro De Empresa");

        javax.swing.GroupLayout jXPanel6Layout = new javax.swing.GroupLayout(jXPanel6);
        jXPanel6.setLayout(jXPanel6Layout);
        jXPanel6Layout.setHorizontalGroup(
            jXPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel6Layout.createSequentialGroup()
                .addGap(295, 295, 295)
                .addComponent(jLabel14)
                .addContainerGap(317, Short.MAX_VALUE))
        );
        jXPanel6Layout.setVerticalGroup(
            jXPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jXPanel6Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel14)
                .addContainerGap())
        );

        jXPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        botonGuardar.setFont(new java.awt.Font("Arial", 1, 12));
        botonGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/srm/vistas/iconos/guardaricono.png"))); // NOI18N
        botonGuardar.setText("Guardar");
        botonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonGuardarActionPerformed(evt);
            }
        });

        botonModificar.setFont(new java.awt.Font("Arial", 1, 12));
        botonModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/srm/vistas/iconos/modificar.png"))); // NOI18N
        botonModificar.setText("Modificar");
        botonModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonModificarActionPerformed(evt);
            }
        });

        botonEmpresas.setFont(new java.awt.Font("Arial", 1, 12));
        botonEmpresas.setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/srm/vistas/iconos/industrias.png"))); // NOI18N
        botonEmpresas.setText("Empresas");
        botonEmpresas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonEmpresasActionPerformed(evt);
            }
        });

        ChecSelect.setFont(new java.awt.Font("Arial", 1, 12));
        ChecSelect.setText("Habilitar Campos");
        ChecSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ChecSelectActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jXPanel1Layout = new javax.swing.GroupLayout(jXPanel1);
        jXPanel1.setLayout(jXPanel1Layout);
        jXPanel1Layout.setHorizontalGroup(
            jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel1Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(ChecSelect)
                .addContainerGap(9, Short.MAX_VALUE))
            .addGroup(jXPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(botonEmpresas, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(jXPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(botonModificar, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
                .addGap(16, 16, 16))
            .addGroup(jXPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(botonGuardar, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
                .addGap(16, 16, 16))
        );
        jXPanel1Layout.setVerticalGroup(
            jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel1Layout.createSequentialGroup()
                .addComponent(ChecSelect)
                .addGap(18, 18, 18)
                .addComponent(botonGuardar)
                .addGap(2, 2, 2)
                .addComponent(botonModificar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                .addComponent(botonEmpresas, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        panelEmpresa.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Datos Generales", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Arial", 0, 12));
        jLabel1.setText("Nombre");

        jLabel3.setFont(new java.awt.Font("Arial", 0, 12));
        jLabel3.setText("Telefono");

        jLabel4.setFont(new java.awt.Font("Arial", 0, 12));
        jLabel4.setText("Correo");

        jLabel5.setFont(new java.awt.Font("Arial", 0, 12));
        jLabel5.setText("Ciudad");

        jLabel6.setFont(new java.awt.Font("Arial", 0, 12));
        jLabel6.setText("Direccion");

        campoDireccion.setColumns(20);
        campoDireccion.setRows(5);
        jScrollPane1.setViewportView(campoDireccion);

        javax.swing.GroupLayout panelEmpresaLayout = new javax.swing.GroupLayout(panelEmpresa);
        panelEmpresa.setLayout(panelEmpresaLayout);
        panelEmpresaLayout.setHorizontalGroup(
            panelEmpresaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelEmpresaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelEmpresaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel6)
                    .addComponent(jLabel4))
                .addGap(60, 60, 60)
                .addGroup(panelEmpresaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(campoCorreo)
                    .addComponent(campoNombre, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1))
                .addGap(40, 40, 40)
                .addGroup(panelEmpresaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(panelEmpresaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(campoTelefono)
                    .addComponent(campoCiudad, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(40, Short.MAX_VALUE))
        );
        panelEmpresaLayout.setVerticalGroup(
            panelEmpresaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelEmpresaLayout.createSequentialGroup()
                .addGroup(panelEmpresaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelEmpresaLayout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addComponent(campoCorreo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(panelEmpresaLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(panelEmpresaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(panelEmpresaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel3)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, panelEmpresaLayout.createSequentialGroup()
                                    .addGroup(panelEmpresaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(campoCiudad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel5))
                                    .addGap(18, 18, 18)
                                    .addComponent(campoTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(panelEmpresaLayout.createSequentialGroup()
                                .addGroup(panelEmpresaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel1)
                                    .addComponent(campoNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addComponent(jLabel4)
                                .addGroup(panelEmpresaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(panelEmpresaLayout.createSequentialGroup()
                                        .addGap(26, 26, 26)
                                        .addComponent(jLabel6))
                                    .addGroup(panelEmpresaLayout.createSequentialGroup()
                                        .addGap(18, 18, 18)
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)))))))
                .addContainerGap(28, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addComponent(panelEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jXPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(12, Short.MAX_VALUE))
            .addComponent(jXPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jXPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(panelEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(32, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonGuardarActionPerformed
        // TODO add your handling code here:
        if(!campoNombre.getText().isEmpty() && !campoCiudad.getText().isEmpty() && !campoTelefono.getText().isEmpty())
        {
        ManejadorEmpresa memp = new ManejadorEmpresa();
        Empresa empresa = null;

       String ciudad = campoCiudad.getText();
       String correo= campoCorreo.getText();
       String direccion = campoDireccion.getText();
       String telefono = campoTelefono.getText();
       String nombre = campoNombre.getText();
       boolean guardo =  memp.crearEmpresa(null, ciudad, correo, direccion, nombre,telefono,  false);
         if(guardo==true){
         JOptionPane.showMessageDialog(null, " Empresa Registrada Exitosamente", "Registro De Empresa", JOptionPane.INFORMATION_MESSAGE);
         limpiar();
         }else{
          JOptionPane.showMessageDialog(null, "No se pudo registrar la Empresa", "Registro De Empresa", JOptionPane.WARNING_MESSAGE);
         }
        }else{
        Control.mensajeJOption("Campo Nombre Empresa es Obligatorio", "Campo Vacio", 15);
        }
    }//GEN-LAST:event_botonGuardarActionPerformed

    private void botonEmpresasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonEmpresasActionPerformed
        // TODO add your handling code here:
        new BusquedaEmpresa(null,true).setVisible(true);
}//GEN-LAST:event_botonEmpresasActionPerformed

    private void botonModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonModificarActionPerformed
        // TODO add your handling code here:
        ManejadorEmpresa mEmp = new ManejadorEmpresa();
        Empresa e = null;
        e = empresast;
        if (campoNombre.getText().isEmpty() || campoTelefono.getText().isEmpty() || campoCiudad.getText().isEmpty()) {
            Control.mensajeJOption(" Existe al menos un un Campo Obligatorio Vacio", "Campos Vacios", 15);
        } else {
            e.setNombre(campoNombre.getText());
            e.setCorreo(campoCorreo.getText());
            e.setCiudad(campoCiudad.getText());
            e.setTelefono(campoTelefono.getText());
            e.setDireccion(campoDireccion.getText());
            boolean estado = mEmp.actualizarEmpresa(e);
            if (estado) {
                JOptionPane.showMessageDialog(null, "Empresa Modificada con Exito", "Modificacion Exitosa", JOptionPane.INFORMATION_MESSAGE);
                ChecSelect.setSelected(false);
               botonGuardar.setEnabled(true);
               botonModificar.setEnabled(false);
                limpiar();
            }else{
              JOptionPane.showMessageDialog(null, " No se completo la modificacion", "Modificacion Incompleta", JOptionPane.INFORMATION_MESSAGE);
            }
        }
    }//GEN-LAST:event_botonModificarActionPerformed

    private void ChecSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ChecSelectActionPerformed
        // TODO add your handling code here:
        if ( ChecSelect.isSelected()){
        Control.disableAndEnabledContainer(panelEmpresa,true);
        }else{
        Control.disableAndEnabledContainer(panelEmpresa,false);
        }

    }//GEN-LAST:event_ChecSelectActionPerformed

    public void limpiar(){
    campoNombre.setText("");
    campoCiudad.setText("");
    campoCorreo.setText("");
    campoDireccion.setText("");
    campoTelefono.setText("");
    }

    /**
    * @param args the command line arguments
    */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                VentanaEmpresa dialog = new VentanaEmpresa(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox ChecSelect;
    private javax.swing.JButton botonEmpresas;
    public static javax.swing.JButton botonGuardar;
    public static javax.swing.JButton botonModificar;
    public static javax.swing.JTextField campoCiudad;
    public static javax.swing.JTextField campoCorreo;
    public static javax.swing.JTextArea campoDireccion;
    public static javax.swing.JTextField campoNombre;
    public static javax.swing.JTextField campoTelefono;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.swingx.JXPanel jXPanel1;
    private org.jdesktop.swingx.JXPanel jXPanel6;
    public static org.jdesktop.swingx.JXPanel panelEmpresa;
    // End of variables declaration//GEN-END:variables

}
